package{
	import flash.text.TextField;

	public class SS{
		
		import flash.utils.ByteArray;
		public function SS(){
		}
		
		public static function trim(input:String):String
		{
			return ltrim(rtrim(input));
		}
		
		public static function ltrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = 0; i < size; i++)
			{
				if(input.charCodeAt(i) > 32)
				{
					return input.substring(i);
				}
			}
			return "";
		}
		
		public static function rtrim(input:String):String
		{
			var size:Number = input.length;
			for(var i:Number = size; i > 0; i--)
			{
				if(input.charCodeAt(i - 1) > 32)
				{
					return input.substring(0, i);
				}
			}
			
			return "";
		}
		
		public static function checkUTF8(ba:ByteArray, len:int = 1000):Boolean {
			//有BOM：EF BB BF
			ba.position = 0;
			var b1:int = ba.readUnsignedByte();
			var b2:int = ba.readUnsignedByte();
			var b3:int = ba.readUnsignedByte();
			if (b1 == 239 && b2 == 187 && b3 == 191){  
				return true;
			}
			ba.position = 0;
			var i:int = 0;
			while (ba.bytesAvailable && i < len) {
				var b:int = ba.readUnsignedByte();
				if (b <= 0x7F || (b >= 0xC2 && b <= 0xF4)) {
					if (b >= 0xC2) {
						var c:int = ba.readUnsignedByte();
						if (c < 0x80 || c > 0xBF) {
							return false;
						}
						if (b >= 0xE0) {
							var d:int = ba.readUnsignedByte();
							if (d < 0x80 || d > 0xBF) {
								return false;
							}
							if (b >= 0xF0) {
								var e:int = ba.readUnsignedByte();
								if (e < 0x80 || e > 0xBF) {
									return false;
								}
							}
						}
					}
				} else {
					return false;
				}
				i ++;
			}
			return true;
		}
		
		
	}
}